var ejs = require("ejs");
var file = require("../models/file.js");
var formidable = require("formidable");
var path = require("path");
var sillytime = require("silly-datetime");
var fs = require("fs");


exports.showIndex = function(req,res,next){
    file.getAllAlbums(function(err,allAlabums){
        if(err){
           next();
            return;
        }
            res.render("index",{
                "albums" : allAlabums
            })

    })
}


exports.showAlabums = function(req,res,next){
    var albumName = req.params.albumName;
    file.getAllImages(albumName,function(err,allImages){
        if(err){
            next();
            return ;
        }
        res.render("album",{
            "alnumname": albumName,
            "baseurl" : req.pathname,
            "allImages" : allImages
        })
    })
}

exports.showUp = function(req,res,next){
    file.getAllAlbums(function(err,allAlabums){
        if(err){
            next();
            return;
        }
           res.render("up",{
                "allAlabums" : allAlabums

            });
    })
}
exports.doPost = function(req,res,next){
    var form = new formidable.IncomingForm();

    var upPath = path.normalize(__dirname + "/../"+"transfer/");
    form.uploadDir = upPath;
    form.parse(req,function(err,fields,files){
        var oldPath = files.imagefile.path;
        var file = fields.folder;
        var time = sillytime.format(new Date(),"YYYYMMDDHHmmss");
        var rannub = parseInt(Math.random()*8999+10000);
        var postfix = path.extname(files.imagefile.name);
        var newPath = path.normalize(__dirname + "/../"+"uploads/"+file+"/"+time+rannub+postfix);
        if(err) {
          next();
          return;
      }
      //   console.log(parseInt(files.imagefile.size));
      // if(parseInt(files.imagefile.size)>51200){
      //       res.send("文件大小不能大于5M");
      //       return;
      // }
      fs.rename(oldPath,newPath,function(err){
          fs.unlink(oldPath,function(err){
          });

          if(err){
              next();
              return;
          }
          res.send("上传成功");
      });
    });
}
exports.newDirectory = function(req,res,next){
    res.render("newdir");
}
exports.newPost = function(req,res,next){
    var newDirname = req.body.dirname;
    var dirPath = path.normalize(__dirname+"/../uploads/"+ newDirname);
    if(newDirname==""){
        res.send("文件夹名不能为空");
        return;
    }

    fs.stat(dirPath,function(err,stat){
        if(stat&&stat.isDirectory()) {
            res.send("文件夹已存在!!");
            return;
        }
        fs.mkdir(dirPath,function(err){
            if(err){
                res.send("文件夹新建失败!!");
                return;
            }
            res.send(newDirname+"文件夹新建成功");
        })
    })
}
exports.managementDir = function(req,res,next){
    file.getAllAlbums(function(err,allAlabums){
        if(err){
            next();
            return;
        }
        res.render("manadir",{
            "albums" : allAlabums
        })

    })

}

exports.renameDir = function(req,res,next){
    var oldPath = req.body.folder;
    var newPath = req.body.newdirname;
    file.renamedirHandle(oldPath,newPath,function(result,bool){
        if(!bool){
            res.send(result);
            return;
        }
        res.send(result);

    })
}

exports.deleteDir = function(req,res){
    var albumName = req.body.folder;

    file.deletedirHandle(albumName,function(result,bool){
        if(!bool){
            res.send(result);
            return;
        }
        res.send(result);
    });
}


